#ifndef THINK_THINK_LOG_DRIVER_SOCKET_H
#define THINK_THINK_LOG_DRIVER_SOCKET_H

static char* think_log_driver_socket =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: luofei614 <weibo.com/luofei614>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think\\log\\driver;\n"
	"\n"
	"use think\\App;\n"
	"\n"
	"/**\n"
	" * github: https://github.com/luofei614/SocketLog\n"
	" * @author luofei614<weibo.com/luofei614>\n"
	" */\n"
	"class Socket\n"
	"{\n"
	"    public $port = 1116; //SocketLog 服务的http的端口号\n"
	"\n"
	"    protected $config = [\n"
	"        // socket服务器地址\n"
	"        \'host\'                => \'localhost\',\n"
	"        // 是否显示加载的文件列表\n"
	"        \'show_included_files\' => false,\n"
	"        // 日志强制记录到配置的client_id\n"
	"        \'force_client_ids\'    => [],\n"
	"        // 限制允许读取日志的client_id\n"
	"        \'allow_client_ids\'    => [],\n"
	"    ];\n"
	"\n"
	"    protected $css = [\n"
	"        \'sql\'      => \'color:#009bb4;\',\n"
	"        \'sql_warn\' => \'color:#009bb4;font-size:14px;\',\n"
	"        \'error\'    => \'color:#f4006b;font-size:14px;\',\n"
	"        \'page\'     => \'color:#40e2ff;background:#171717;\',\n"
	"        \'big\'      => \'font-size:20px;color:red;\',\n"
	"    ];\n"
	"\n"
	"    protected $allowForceClientIds = []; //配置强制推送且被授权的client_id\n"
	"\n"
	"    /**\n"
	"     * 构造函数\n"
	"     * @param array $config 缓存参数\n"
	"     * @access public\n"
	"     */\n"
	"    public function __construct(array $config = [])\n"
	"    {\n"
	"        if (!empty($config)) {\n"
	"            $this->config = array_merge($this->config, $config);\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 调试输出接口\n"
	"     * @access public\n"
	"     * @param array     $log 日志信息\n"
	"     * @return bool\n"
	"     */\n"
	"    public function save(array $log = [], $append = false)\n"
	"    {\n"
	"        if (!$this->check()) {\n"
	"            return false;\n"
	"        }\n"
	"        $trace = [];\n"
	"        if (App::$debug) {\n"
	"            $runtime    = round(microtime(true) - THINK_START_TIME, 10);\n"
	"            $reqs       = $runtime > 0 ? number_format(1 / $runtime, 2) : \'∞\';\n"
	"            $time_str   = \' [运行时间：\' . number_format($runtime, 6) . \'s][吞吐率：\' . $reqs . \'req/s]\';\n"
	"            $memory_use = number_format((memory_get_usage() - THINK_START_MEM) / 1024, 2);\n"
	"            $memory_str = \' [内存消耗：\' . $memory_use . \'kb]\';\n"
	"            $file_load  = \' [文件加载：\' . count(get_included_files()) . \']\';\n"
	"\n"
	"            if (isset($_SERVER[\'HTTP_HOST\'])) {\n"
	"                $current_uri = $_SERVER[\'HTTP_HOST\'] . $_SERVER[\'REQUEST_URI\'];\n"
	"            } else {\n"
	"                $current_uri = \'cmd:\' . implode(\' \', $_SERVER[\'argv\']);\n"
	"            }\n"
	"            // 基本信息\n"
	"            $trace[] = [\n"
	"                \'type\' => \'group\',\n"
	"                \'msg\'  => $current_uri . $time_str . $memory_str . $file_load,\n"
	"                \'css\'  => $this->css[\'page\'],\n"
	"            ];\n"
	"        }\n"
	"\n"
	"        foreach ($log as $type => $val) {\n"
	"            $trace[] = [\n"
	"                \'type\' => \'groupCollapsed\',\n"
	"                \'msg\'  => \'[ \' . $type . \' ]\',\n"
	"                \'css\'  => isset($this->css[$type]) ? $this->css[$type] : \'\',\n"
	"            ];\n"
	"            foreach ($val as $msg) {\n"
	"                if (!is_string($msg)) {\n"
	"                    $msg = var_export($msg, true);\n"
	"                }\n"
	"                $trace[] = [\n"
	"                    \'type\' => \'log\',\n"
	"                    \'msg\'  => $msg,\n"
	"                    \'css\'  => \'\',\n"
	"                ];\n"
	"            }\n"
	"            $trace[] = [\n"
	"                \'type\' => \'groupEnd\',\n"
	"                \'msg\'  => \'\',\n"
	"                \'css\'  => \'\',\n"
	"            ];\n"
	"        }\n"
	"\n"
	"        if ($this->config[\'show_included_files\']) {\n"
	"            $trace[] = [\n"
	"                \'type\' => \'groupCollapsed\',\n"
	"                \'msg\'  => \'[ file ]\',\n"
	"                \'css\'  => \'\',\n"
	"            ];\n"
	"            $trace[] = [\n"
	"                \'type\' => \'log\',\n"
	"                \'msg\'  => implode(\"\\n\", get_included_files()),\n"
	"                \'css\'  => \'\',\n"
	"            ];\n"
	"            $trace[] = [\n"
	"                \'type\' => \'groupEnd\',\n"
	"                \'msg\'  => \'\',\n"
	"                \'css\'  => \'\',\n"
	"            ];\n"
	"        }\n"
	"\n"
	"        $trace[] = [\n"
	"            \'type\' => \'groupEnd\',\n"
	"            \'msg\'  => \'\',\n"
	"            \'css\'  => \'\',\n"
	"        ];\n"
	"\n"
	"        $tabid = $this->getClientArg(\'tabid\');\n"
	"        if (!$client_id = $this->getClientArg(\'client_id\')) {\n"
	"            $client_id = \'\';\n"
	"        }\n"
	"\n"
	"        if (!empty($this->allowForceClientIds)) {\n"
	"            //强制推送到多个client_id\n"
	"            foreach ($this->allowForceClientIds as $force_client_id) {\n"
	"                $client_id = $force_client_id;\n"
	"                $this->sendToClient($tabid, $client_id, $trace, $force_client_id);\n"
	"            }\n"
	"        } else {\n"
	"            $this->sendToClient($tabid, $client_id, $trace, \'\');\n"
	"        }\n"
	"        return true;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 发送给指定客户端\n"
	"     * @author Zjmainstay\n"
	"     * @param $tabid\n"
	"     * @param $client_id\n"
	"     * @param $logs\n"
	"     * @param $force_client_id\n"
	"     */\n"
	"    protected function sendToClient($tabid, $client_id, $logs, $force_client_id)\n"
	"    {\n"
	"        $logs = [\n"
	"            \'tabid\'           => $tabid,\n"
	"            \'client_id\'       => $client_id,\n"
	"            \'logs\'            => $logs,\n"
	"            \'force_client_id\' => $force_client_id,\n"
	"        ];\n"
	"        $msg     = @json_encode($logs);\n"
	"        $address = \'/\' . $client_id; //将client_id作为地址， server端通过地址判断将日志发布给谁\n"
	"        $this->send($this->config[\'host\'], $msg, $address);\n"
	"    }\n"
	"\n"
	"    protected function check()\n"
	"    {\n"
	"        $tabid = $this->getClientArg(\'tabid\');\n"
	"        //是否记录日志的检查\n"
	"        if (!$tabid && !$this->config[\'force_client_ids\']) {\n"
	"            return false;\n"
	"        }\n"
	"        //用户认证\n"
	"        $allow_client_ids = $this->config[\'allow_client_ids\'];\n"
	"        if (!empty($allow_client_ids)) {\n"
	"            //通过数组交集得出授权强制推送的client_id\n"
	"            $this->allowForceClientIds = array_intersect($allow_client_ids, $this->config[\'force_client_ids\']);\n"
	"            if (!$tabid && count($this->allowForceClientIds)) {\n"
	"                return true;\n"
	"            }\n"
	"\n"
	"            $client_id = $this->getClientArg(\'client_id\');\n"
	"            if (!in_array($client_id, $allow_client_ids)) {\n"
	"                return false;\n"
	"            }\n"
	"        } else {\n"
	"            $this->allowForceClientIds = $this->config[\'force_client_ids\'];\n"
	"        }\n"
	"        return true;\n"
	"    }\n"
	"\n"
	"    protected function getClientArg($name)\n"
	"    {\n"
	"        static $args = [];\n"
	"\n"
	"        $key = \'HTTP_USER_AGENT\';\n"
	"\n"
	"        if (isset($_SERVER[\'HTTP_SOCKETLOG\'])) {\n"
	"            $key = \'HTTP_SOCKETLOG\';\n"
	"        }\n"
	"\n"
	"        if (!isset($_SERVER[$key])) {\n"
	"            return;\n"
	"        }\n"
	"        if (empty($args)) {\n"
	"            if (!preg_match(\'/SocketLog\\((.*?)\\)/\', $_SERVER[$key], $match)) {\n"
	"                $args = [\'tabid\' => null];\n"
	"                return;\n"
	"            }\n"
	"            parse_str($match[1], $args);\n"
	"        }\n"
	"        if (isset($args[$name])) {\n"
	"            return $args[$name];\n"
	"        }\n"
	"        return;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * @param string $host - $host of socket server\n"
	"     * @param string $message - 发送的消息\n"
	"     * @param string $address - 地址\n"
	"     * @return bool\n"
	"     */\n"
	"    protected function send($host, $message = \'\', $address = \'/\')\n"
	"    {\n"
	"        $url = \'http://\' . $host . \':\' . $this->port . $address;\n"
	"        $ch  = curl_init();\n"
	"        curl_setopt($ch, CURLOPT_URL, $url);\n"
	"        curl_setopt($ch, CURLOPT_POST, true);\n"
	"        curl_setopt($ch, CURLOPT_POSTFIELDS, $message);\n"
	"        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n"
	"        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);\n"
	"        curl_setopt($ch, CURLOPT_TIMEOUT, 10);\n"
	"        $headers = [\n"
	"            \"Content-Type: application/json;charset=UTF-8\",\n"
	"        ];\n"
	"        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //设置header\n"
	"        return curl_exec($ch);\n"
	"    }\n"
	"\n"
	"}\n"
	"\n"
;
#endif